<?php

//view of teacher is in view/teacher folder
//content: create.php - create new topic
//		   list.php - view list students enrolled
//		   modify.php - modify topic
//         mytopic.php - view list my topics

class Teacher extends CI_Controller{
	function __construct(){
		parent::__construct();
	}
	
	//view index page for teacher
	function index(){
		if ($this->session->userdata('logged_in')==false){
			redirect('/login');
		}
		redirect('teacher/mytopic');
	}
	
	//view teacher's topics
	function mytopic(){
		if ($this->session->userdata('logged_in')==false){
			redirect('/login');
		}
		$teacherId = $this->session->userdata('user_id');
		$this->load->model('thesismodel');
		$data['rows'] = $this->thesismodel->getTopic($teacherId);
		$data['title'] = 'My topics';
		$this->load->view('teacher/mytopic',$data);
	}
	
	//modify topic
	function modify(){
		if ($this->session->userdata('logged_in')==false){
			redirect('/login');
		}
		
		$this->load->model('thesismodel');
		$id = $this->session->userdata('topicId');
	//	$id = $this->uri->segment(3);
		$data['title'] = "Edit";
		$data['rows'] = $this->thesismodel->searchById($id);
		$this->load->library('form_validation');
		
		$config = array(
			array(
				'field' => 'name',
				'rules' => 'required|trim'
			),
			array(
				'field' => 'description',
				'rules' => 'required|trim'
			),
			array(
				'field' => 'area',
				'rules' => 'required|trim'
			),
			array(
				'field' => 'quantity',
				'rules' => 'required|trim|numeric'
			),
			array(
				'field' => 'type',
				'rules' => 'required|trim'
			)
		);
		
		$this->form_validation->set_rules($config);
		
		if ($this->form_validation->run()){
			$content = array(
				'name' =>$this->input->post('name'),
				'description' => $this->input->post('description'),
				'quantity_students' => $this->input->post('quantity'),
				'thesisType' => $this->input->post('type'),
				'researchArea' => $this->input->post('area')
			);
			
			$this->thesismodel->update($id,$content);
			redirect('teacher/mytopic');
		}
		else
			$this->load->view('teacher/modify',$data);
	}
	
	//delete topic
	function delete(){
		if ($this->session->userdata('logged_in')==false){
			redirect('/login');
		}
		
		$thesisId = $this->uri->segment(3);
		$this->load->model('liststudent');
		$this->liststudent->deleteId($thesisId);
		
		$this->load->model('thesismodel');
		$this->thesismodel->delete($thesisId);
		redirect('teacher/mytopic');
	}
	
	//create topic
	function create(){
		//if not logged in redirect to login page
		if ($this->session->userdata('logged_in')==false){
			redirect('/login');
		}
		
		//set form validation
		$this->load->library('form_validation');
		$config = array(
			array(
				'field' => 'name',
				'rules' => 'required|trim'
			),
			array(
				'field' => 'description',
				'rules' => 'required|trim'
			),
			array(
				'field' => 'area',
				'rules' => 'required|trim'
			),
			array(
				'field' => 'type',
				'rules' => 'required|trim'
			),
			array(
				'field' => 'quantity',
				'rules' => 'required|trim|numeric'
			)
		);
		
		$data['title'] = "Create topic";
		
		//set rules
		$this->form_validation->set_rules($config);
		
		//if form run success add to database and direct to mytopic else direct again 
		if ($this->form_validation->run()){
			$this->load->model('thesismodel');
			$this->thesismodel->add();
			redirect('teacher/mytopic');
		}
		else
			$this->load->view('teacher/create',$data);
	}
	
	//view list students of thesis
	function viewlist(){
		$this->load->model('liststudent');
		$data['title'] = 'List';
		$data['rows'] = $this->liststudent->getlist();
		$this->load->view('teacher/list',$data);
	}
	
	//decide accept or reject
	function decide(){
		$decision = $this->uri->segment(3);
		$this->load->model('liststudent');
		$studentId = $this->session->userdata('studentId');
		$status = $this->liststudent->checkStatus($studentId);
		$this->load->model('thesismodel');
		$this->thesismodel->updateQuantity($decision,$status);
		$this->liststudent->update($decision);
		redirect('teacher/viewlist');
	}
}
?>